#ifndef MYGLOBAL_H
#define MYGLOBAL_H
#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <avr/boot.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
//#include <compat/deprecated.h>

#include "avrlibdefs.h"		// global AVRLIB defines
#include "avrlibtypes.h"		// global AVRLIB types definitions






//int main(void) __attribute__((__noreturn__));

typedef unsigned char uchar;

#define MYLIBSECTION	__attribute__((section(".mylib")))
#define BOOTSECTION		__attribute__((section(".boot")))
#define EEPROMSECTION	__attribute__((section(".eeprom")))



extern void __mulsi3 ( void ) MYLIBSECTION;
//extern void __ctors_end(void) __attribute__((__noreturn__));

// project/system dependent defines

// CPU clock speed
//#define F_CPU        16000000               		// 16MHz processor
//#define F_CPU        14745000               		// 14.745MHz processor
//#define F_CPU        8000000               		// 8MHz processor
//#define F_CPU        7372800               		// 7.37MHz processor
//#define F_CPU        4000000               		// 4MHz processor
//#define F_CPU        3686400               		// 3.69MHz processor
//#define F_CPU        11059200

#define CYCLES_PER_US ((F_CPU+500000)/1000000) 	// cpu cycles per microsecond

#define MSELREAD 	(~PIND) & (1<<PIND2)

#define LED_INIT	sbi	(DDRB,DDB3)
#define LED_OFF 	sbi (PORTB,PB3)
#define LED_ON		cbi (PORTB,PB3)

//#define ERROR_INIT	DDRD |= 1<<DDD5
//#define ERROR_ON 	sbi (PORTD,PD5)
//#define ERROR_OFF	cbi (PORTD,PD5)

//#define FLAG_INIT 	DDRD |= 1<<DDD4
//#define FLAG_ON 	sbi (PORTD,PD4)
//#define FLAG_OFF	cbi (PORTD,PD4)


//DDRC |= (1<<DDC1);
//PORTC |= (1<<PC1);


typedef struct {
	uint8_t lo;
    uint8_t hi;
} byte_t;

typedef union {			//// определение объеденения слова с байтами
	uint16_t word;
	byte_t byte;
} word_t;

//typedef struct {		////	структура копии модбаса в еепроме копия А, инверсная копия Б, и флаги.
//    myword A[64];
//    myword B[64];
//    uint8_t F[64];
//} STEEMODBUS;

//typedef struct {
//	STEEMODBUS	MODBUS;
//} STEE;

typedef struct {		////	структура копии модбаса в еепроме копия А, инверсная копия Б, и флаги.
	word_t		A[64];
	word_t		B[64];
	uint8_t		F[64];
} MODBUS_t;

/*
typedef struct {

};

typedef struct {
   eval_type type;
   union {
      struct {
         LPVOID value;
      } as_object;
      struct {
         LPTSTR value;
         UINT length;
         UINT size;
      } as_string;
      struct {
         LPNODE first;
         LPNODE last;
      } as_bi_tree;
   } content;
} root_type;

root_type a;
a.content.as_object.value = NULL;

*/


#define EEPROM_FLAG_OK		5
#define EEPROM_FLAG_BEGIN	1
#define EEPROM_FLAG_2		2
#define EEPROM_FLAG_3		3
#define EEPROM_FLAG_4		4

/* ----------------------- Defines ------------------------------------------*/
#define	INLINE                      inline
///#define PR_BEGIN_EXTERN_C           extern "C" {
///#define	PR_END_EXTERN_C             }




#ifndef CRITICAL_SECTION_START
#define CRITICAL_SECTION_START	UCHAR _sreg = SREG; cli()
#define CRITICAL_SECTION_END	SREG = _sreg
#endif

#ifndef SREG_SAVE
#define SREG_SAVE	volatile register UCHAR _sreg = SREG
#define SREG_RESTORE	SREG = _sreg
#endif


#define ENTER_CRITICAL_SECTION( )   cli()
#define EXIT_CRITICAL_SECTION( )    sei()

#define assert( x )

//typedef char    BOOL;

//typedef unsigned char UCHAR;
//typedef char    CHAR;

//typedef unsigned short USHORT;
//typedef short   SHORT;

//typedef unsigned long ULONG;
//typedef long    LONG;

//#ifndef TRUE
//#define TRUE            1
//#endif

//#ifndef FALSE
//#define FALSE           0
//#endif



/* ----------------------- AVR platform specifics ---------------------------*/

#if defined (__AVR_ATmega88__)
#define UCSRB           UCSR0B
#define TXEN            TXEN0
#define RXEN            RXEN0
#define RXCIE           RXCIE0
#define TXCIE           TXCIE0
#define UDRE            UDRE0
#define UBRR            UBRR0
#define UCSRC           UCSR0C
#define UPM1            UPM01
#define UPM0            UPM00
#define UCSZ0           UCSZ00
#define UCSZ1           UCSZ01
#define UDR             UDR0
#define SIG_UART_TRANS  SIG_USART_TRANS

#elif defined (__AVR_ATmega88A__)
#define UCSRB           UCSR0B
#define TXEN            TXEN0
#define RXEN            RXEN0
#define RXCIE           RXCIE0
#define TXCIE           TXCIE0
#define UDRE            UDRE0
#define UBRR            UBRR0
#define UCSRC           UCSR0C
#define UPM1            UPM01
#define UPM0            UPM00
#define UCSZ0           UCSZ00
#define UCSZ1           UCSZ01
#define UDR             UDR0
#define SIG_UART_TRANS  SIG_USART_TRANS

#elif defined (__AVR_ATmega169__)

#define SIG_UART_TRANS  SIG_USART_TRANS

#elif defined (__AVR_ATmega8__)
#define UBRR            UBRRL
#define TCCR1C          TCCR1A  /* dummy */
#define TIMSK1          TIMSK
#define TIFR1           TIFR
#define SIG_USART_DATA  SIG_UART_DATA
#define SIG_USART_RECV  SIG_UART_RECV

#elif defined (__AVR_ATmega16__)
#define UBRR            UBRRL
#define TCCR1C          TCCR1A  /* dummy */
#define TIMSK1          TIMSK
#define TIFR1           TIFR

#elif defined (__AVR_ATmega32__)
#define UBRR            UBRRL
#define TCCR1C          TCCR1A  /* dummy */
#define TIMSK1          TIMSK
#define TIFR1           TIFR

#elif defined (__AVR_ATmega128__)
#define UCSRB           UCSR0B
#define UBRR            UBRR0L
#define UDR             UDR0
#define TIMSK1          TIMSK
#define TIFR1           TIFR
#define SIG_UART_TRANS  SIG_USART0_TRANS
#define SIG_USART_DATA  SIG_USART0_DATA
#define SIG_USART_RECV  SIG_USART0_RECV
#define UCSZ0           UCSZ00
#define UCSZ1           UCSZ01
#define UPM0            UPM00
#define UPM1            UPM01
#define UCSRC           UCSR0C

#endif

/* ----------------------- RS485 specifics ----------------------------------*/
















/* Errorcodes used by all function in the protocol stack. */
typedef enum {
    MB_ENOERR,                  /*!< no error. */
    MB_ENOREG,                  /*!< illegal register address. */
    MB_EINVAL,                  /*!< illegal argument. */
    MB_EPORTERR,                /*!< porting layer error. */
    MB_ENORES,                  /*!< insufficient resources. */
    MB_EIO,                     /*!< I/O error. */
    MB_EILLSTATE,               /*!< protocol stack in illegal state. */
    MB_ETIMEDOUT                /*!< timeout error occurred. */
} eMBErrorCode;

typedef enum {
    EV_READY,                   // Startup finished.
    EV_FRAME_RECEIVED,          // Frame received.
    EV_EXECUTE,                 // Execute function.
    EV_FRAME_SENT               // Frame sent.
} eMBEventType;





#endif
